<template>
  <div class="system-user-dialog-container">
    <el-dialog :close-on-click-modal="false" :title="dataForm.userId ? $t('common.editBtn') : $t('common.addBtn')"
      draggable v-model="visible" width="600" @close="onClose">
      <el-form :model="dataForm" :rules="dataRules" label-width="100px" ref="dataFormRef" v-loading="loading">
        <el-row :gutter="20">
          <el-col :span="12" class="mb20">
            <el-form-item prop="productionLineCode">
              <template #label>选择产线
                <tip content="选择产线" />
              </template>
              <ProductionSelect style="width: 150px"  v-if="dataForm.productionLineCode" v-model="dataForm.productionLineCode" 
                @change="changeproductionLineCode" res="ProductionSelectRef"/>
              <ProductionSelect style="width: 150px"  v-else v-model="dataForm.productionLineCode" 
                @change="changeproductionLineCode" res="ProductionSelectRef"/>
            </el-form-item>

          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('productlineUser.ShiftName')" prop="shiftCode">
              <el-select v-model="dataForm.shiftCode" placeholder="Select" style="width: 160px">
                <el-option v-for="item in shiftOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('message.userNo')" prop="WorkNumber">
              <template #label>选择人员
                <tip content="选择人员" />
              </template>
              <UserSelect style="width: 150px" v-if="dataForm.WorkNumber" v-model="dataForm.WorkNumber" 
                @change="changeUser" res="UserSelectRef"/>
              <UserSelect style="width: 150px" v-else v-model="dataForm.WorkNumber" 
                @change="changeUser" res="UserSelectRef"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onClose" icon="close">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button @click="onSubmit" type="primary" icon="check" :disabled="loading">{{
            $t('common.confirmButtonText')
          }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="spDialog" setup>
import { useMessage } from '/@/hooks/message';
import { ref, reactive, watch } from 'vue';
import ProductionSelect from "/@/components/mes/select/ProductionSelect.vue";
import UserSelect from "/@/components/mes/select/UserSelect.vue";
import { AddProductionLineUsers, GetAllUsers } from '/@/api/platform/mes/productlineUser';
import type { CheckboxValueType } from 'element-plus'

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
// 通知父组件刷新
const emit = defineEmits(['refresh']);

const dataForm = reactive({
    WorkNumber: '',
    productionLineCode: '',
    productionLineName: ''
});
const dataRules = ref({
  shiftCode: [
    { required: true, message: t('bar.cannotEmpty'), trigger: 'blur' },
  ],
  productionLineCode: [
    { required: true, message: t('bar.cannotEmpty'), trigger: 'blur' },
  ],
  WorkNumber: [
    { required: true, message: t('bar.cannotEmpty'), trigger: 'blur' },
  ]
});
const shiftOptions = ref([
  { label: '早班', value: 'MorningShift' },
  { label: '晚班', value: 'EveningShift' },
]);
// 打开弹窗
const openDialog = async (ProductionLineCode: string,ProductionLineName:string) => {
  console.log(ProductionLineCode,'ProductionLineCode');
  
  visible.value = true;
  // useMessage().success(ProductionLineCode);
  dataForm.productionLineCode=ProductionLineCode;
  dataForm.productionLineName=ProductionLineName;
  // state.queryForm.ProductionLineName
  console.log(dataForm,'----------------------');
  // // 重置表单数据
  // nextTick(() => {
  //   dataFormRef.value?.resetFields();
  //     console.log(dataForm.value,'----------------------');
  // });

};

// 初始化数据
onMounted(() => {
  GetAllUsers().then((res) => {
    dataForm.workUserList.value = res.data;
  });
});
// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  console.log(dataForm.Users);
  try {
    loading.value = true;
    await AddProductionLineUsers(dataForm).then((res) => {
      if (!res.ok) {
        useMessage().error(res.data);
      }
    });
    visible.value = false; // 关闭弹窗
    emit('refresh');
  } catch (error: any) {
    useMessage().error(error.msg);
  } finally {
    loading.value = false;
  }
};
const ProductionSelectRef = ref();
const UserSelectRef = ref();
const onClose = () => {
	visible.value = false;
	// dataFormRef.value?.resetFields();
	ProductionSelectRef.value?.clear();
  UserSelectRef.value?.clear();
  dataForm.productionLineCode='';
  dataForm.productionLineName='';
  dataForm.WorkNumber='';
};
const changeproductionLineCode = (val) => {
  if (val) {
    dataForm.ProductionLineCode = val.productionLineCode;
    dataForm.ProductionLineName = val.productionLineName;
  } else {
    dataForm.ProductionLineCode = '';
    dataForm.ProductionLineName = '';
  }
};
const changeUser = (val) => {
  if (val) {
    dataForm.WorkNumber = val.WorkNumber;
  } else {
    dataForm.WorkNumber = '';
  }
};

// 暴露变量
defineExpose({
  openDialog,
});

</script>